Skip to contents

Many modelling packages split a formula's right-hand side into sections with |: fixed effects and instruments in fixest::feols() (y ~ x | fe | endo ~ inst), the precision model in betareg::betareg(), the zero-inflation model in pscl::zeroinfl(), instruments in ivreg::ivreg(). Parts are those sections. They are rendered in order after the main terms, as main | part 1 | part 2; what each one means is up to the modelling function.

Applied to a spec, the term helpers (add_terms, update.model_spec and the rest) change its main terms and leave its parts alone. To change a part given as a model_spec, apply them to the part through modify_part.

A | inside a term, as in lme4's (1 | g), is not a part: write it as an ordinary term.

Usage

set_parts(spec, ...)

Arguments

spec

A model_spec object.

...

Parts, in order, replacing any the spec has. Each is a model_spec (which modify_part can later change with the term helpers), a formula (one-sided, ~ fe1 + fe2, or two-sided, endo ~ inst) or a string. Formulas are stored as strings. Call set_parts(spec) without parts to remove them all.

Value

A new model_spec with the parts set.

Examples

# fixest: regressors | fixed effects
m <- model_spec(x1, x2) |> set_parts(model_spec(firm, year))
to_formula(m, "y")
#> y ~ x1 + x2 | firm + year
#> <environment: 0x62b09bf4bbb8>

# fixest: regressors | fixed effects | IV
set_parts(model_spec(x1), ~ firm, endo ~ inst) |> to_formula("y")
#> y ~ x1 | firm | endo ~ inst
#> <environment: 0x62b09bfbf350>

# Parts survive changes to the main terms
m |> add_terms(x3) |> to_formula("y")
#> y ~ x1 + x2 + x3 | firm + year
#> <environment: 0x62b09c0485a8>